/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf;

import ejb.ClienteFacade;
import entity.Cliente;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author mbvierci
 */
@ManagedBean
@RequestScoped
public class ClienteBean {

    @EJB
    private ClienteFacade clienteFacade;
    private Cliente cliente = new Cliente();
    private List<Cliente> clientes;
    private long id;

    public void initClient() {
        cliente = clienteFacade.find(id);
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    /**
     * Creates a new instance of ClienteBean
     */
    public ClienteBean() {
    }

    public Cliente getCliente() {
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    public ClienteFacade getClienteFacade() {
        return clienteFacade;
    }

    public void setClienteFacade(ClienteFacade clienteFacade) {
        this.clienteFacade = clienteFacade;
        clientes = clienteFacade.findAll();
    }

    public List<Cliente> getClientes() {
        return clientes;
    }

    public void setClientes(List<Cliente> clientes) {
        this.clientes = clientes;
    }

    public void crearCliente() {
        clienteFacade.create(cliente);
        clientes.add(cliente);
        cliente = new Cliente();
    }

    public void deleteCliente() {

        clienteFacade.remove(cliente);
    }

    public void editarCliente() {

        clienteFacade.edit(cliente);

    }

    public String getClienteParam(FacesContext fc) {

        Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
        return params.get("cliente");

    }
}
